﻿using System;
using System.Collections.Generic;
using System.Resources;
using System.Text;
using com.lover.astd.common.config;
using com.lover.astd.common.logic;
using com.lover.astd.common.model;
using com.lover.astd.common.model.building;
using com.lover.astd.common.model.enumer;

namespace com.lover.astd.common.logicexe.hero
{
    public class HeroTrainExe : ExeBase
    {
        public HeroTrainExe() { _name = ConfigStrings.S_HeroTrain; _readable = ConfigStrings.SR_HeroTrain; }
        public void getAllHeroes()
        {
            ServiceFactory.getHeroManager().getAllHeros(_proto, _logger, ref _user);
            refreshUi();
        }
        public override void init_data() { getAllHeroes(); }
        public override long execute()
        {
            Dictionary<string, string> conf = getConfig();
            if (!conf.ContainsKey(ConfigStrings.enabled) || !conf[ConfigStrings.enabled].ToLower().Equals("true"))
            {
                if (_user.isActivityRunning(common.model.enumer.ActivityType.GiftEvent))
                {
                    if (!conf.ContainsKey(ConfigStrings.giftevent) || !conf[ConfigStrings.giftevent].ToLower().Equals("true")) return an_hour_later();
                }
                else return an_hour_later();
            }
            bool have_hero_to_train = false;
            getAllHeroes();

            int credit_available = getCreditAvailable();
            if (_user.TufeiNumber == 0 && _user.TufeiCredit > credit_available) return next_hour();

            if (conf.ContainsKey(ConfigStrings.train_heros) && conf[ConfigStrings.train_heros] != "")
            {
                int user_level = _user.Level;
                string train_heros = conf[ConfigStrings.train_heros];
                string[] train_heroids = train_heros.Split(',');
                if (train_heroids.Length > 0)
                {
                    int i = 0, len = train_heroids.Length, j = 0;
                    List<Hero> heroes = _user.Heroes;
                    List<Hero> hs = new List<Hero>();
                    foreach (Hero hero in heroes)
                    {
                        for (i = 0; i < len; i++)
                        {
                            if (train_heroids[i].Equals(hero.Id.ToString()))
                            {
                                if (hero.TrainPositionId > 0) hs.Add(hero);
                                break;
                            }
                        }
                    }
                    if (hs.Count > 0)
                    {
                        have_hero_to_train = true;
                        bool success_trained = true;
                        int loop_count = 0;
                        while ((!_user.TuFeiCdFlag || _user.TuFeiCd == 0) && success_trained && loop_count < 20)
                        {
                            loop_count++;
                            // 查看是否有已经训练完毕的武将, 如果有, 那么从列表中移除
                            Hero h = null;
                            for (i = 0; i < hs.Count; i++)
                            {
                                if (hs[i].TrainPositionId <= 0 || hs[i].Level >= user_level)
                                {
                                    hs.RemoveAt(i); i--; len--;
                                }
                            }
                            for (i = 0; i < hs.Count - 1; i++)
                            {
                                for (j = i + 1; j < hs.Count; j++)
                                {
                                    if (hs[i].Level > hs[j].Level)
                                    {
                                        h = hs[i]; hs[i] = hs[j]; hs[j] = h;
                                    }
                                }
                            }
                            if (_user.TufeiNumber == 0 && _user.TufeiCredit > credit_available) return next_hour();
                            if (hs != null && hs.Count > 0) success_trained = ServiceFactory.getHeroManager().trainHero(_proto, _logger, _user, hs[0].Id);
                        }

                    }
                }
            }
            if (_user.TuFeiCdFlag && _user.TuFeiCd > 0)
            {
                bool removecd = conf.ContainsKey(ConfigStrings.nocd) && conf[ConfigStrings.nocd].ToLower().Equals("true");
                int gold_available = getGoldAvailable();
                if (removecd && gold_available >= 3)
                {
                    if (ServiceFactory.getHeroManager().removeTrainCd(_proto, _logger)) return immediate();
                    else return next_halfhour();
                }
                return _user.TuFeiCd;
            }
            else
            {
                if (have_hero_to_train) return immediate();
                else return next_halfhour();
            }
        }
    }
}
